package com.leapp.sk.viewdelegate.classs

import android.content.Context
import android.content.Intent
import android.support.v4.content.ContextCompat
import android.support.v4.widget.SwipeRefreshLayout
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.ProgressBar
import android.widget.TextView
import cc.vv.lkbasecomponent.util.LKScreenUtil
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.PopupWindowUtil
import com.example.mvp.view.RefreshListView
import com.example.mvp.view.RefreshRecyclerView
import com.example.mvp.view.SKTopBar
import com.example.mvp.view.imgselector.ImageSelector
import com.example.util.ISTECHER
import com.leapp.sk.R
import com.leapp.sk.bean.ClassAlbumDetialEntity
import com.leapp.sk.ui.activity.album.EditAlbumActivity
import com.leapp.sk.ui.activity.classs.ClassAlbumDetialActivity
import com.leapp.sk.ui.activity.classs.ClassDetialActivity
import com.leapp.sk.ui.activity.classs.GroupManagerActivity
import com.leapp.sk.ui.adapter.ClassAlbumDetialAdapter

/**
 * 班级相册视图托管类
 * @author  chaohaipeng  on 2018/8/6.
 */
class ClassAlbumDetialDelegate: BaseAppDelegate(){
    private var popupWindow: PopupWindow? = null
    /**
     * 适配器
     */
    private var classAlbumDetialAdapter: ClassAlbumDetialAdapter? = null


    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_class_album_detial
    }

    /**
     * 初始化适配器
     */
    fun initAdapter(context: Context,list: List<ClassAlbumDetialEntity>,listener:SwipeRefreshLayout.OnRefreshListener){
        val rv_acad_list = getView<RefreshListView>(R.id.rv_acad_list)

        val srl_acad_swipe = getView<SwipeRefreshLayout>(R.id.srl_acad_swipe)
        srl_acad_swipe.setOnRefreshListener(listener)
        srl_acad_swipe.setColorSchemeColors(ContextCompat.getColor(getActivity(), R.color.color_19AD19))
         classAlbumDetialAdapter = ClassAlbumDetialAdapter(context, list)
        rv_acad_list.adapter=classAlbumDetialAdapter

        popupWindow = PopupWindowUtil.getInstance().initView(initFootView(context), LKScreenUtil.getScreenWidth(), LinearLayout.LayoutParams.WRAP_CONTENT)
    }

    private fun initFootView(context: Context): View {
        val view  = LayoutInflater.from(context).inflate(R.layout.layout_album_edit_popupwindow, null)
        val ll_laep_edit:LinearLayout = view.findViewById(R.id.ll_laep_edit)
       val ll_laep_album:LinearLayout = view.findViewById(R.id.ll_laep_album)

        ll_laep_edit.setOnClickListener(View.OnClickListener {
         val intent=Intent(context, EditAlbumActivity::class.java)
            context.startActivity(intent)

        })
        ll_laep_album.setOnClickListener(View.OnClickListener {
            popupWindow?.dismiss()
            pickImage()
        })

        return view
    }

    fun notifyAdapter(){
        classAlbumDetialAdapter?.notifyDataSetChanged()
    }


    /**
     * 照片选择
     */
    private fun pickImage() {
        ImageSelector.getInstance()
                .setSelectModel(ImageSelector.MULTI_MODE)
                .setMaxCount(9)
                .setGridColumns(3)
                .setShowCamera(true)
                .startSelect(getActivity())
    }
    /**
     * 停止刷新
     */
    fun stopSwipRefresh(){
        val srl_acad_swipe = getView<SwipeRefreshLayout>(R.id.srl_acad_swipe)
        srl_acad_swipe.isRefreshing=false
    }
    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        val jtb_acad= getView<SKTopBar>(R.id.jtb_acad)
        if (ISTECHER) {
            jtb_acad.setRightBtnImage(R.mipmap.icon_album_edit_non)
            jtb_acad.setRightBtnImageShow(true)
        }
        if (!TextUtils.isEmpty(title)){
            jtb_acad.setTitle(title)
        }
        setEvent(jtb_acad)
    }
    /**
     * 设置设置事件
     */
  private  fun setEvent(topbar:SKTopBar){
        //消失监听听
        popupWindow?.setOnDismissListener(object:PopupWindow.OnDismissListener{
            override fun onDismiss() {
                topbar.setRightBtnImage(R.mipmap.icon_album_edit_non)
            }

        })
        topbar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<ClassAlbumDetialActivity>().finish()
            }
            override fun rightOnClick() {
                if (ISTECHER) {
                    val showing = popupWindow?.isShowing
                    if (showing!=null){
                        if (showing){
                            popupWindow?.dismiss()

                        }else{

                            popupWindow?.showAsDropDown(topbar,0,0)
                            topbar.setRightBtnImage(R.mipmap.icon_album_close)
                        }
                    }
                }

            }

        })
    }
}